// models/index.js
const sequelize = require('../config/database');
const { DataTypes } = require('sequelize');

const User = require('./User')(sequelize, DataTypes);
const Category = require('./Category')(sequelize, DataTypes);
const Item = require('./Item')(sequelize, DataTypes);
const ItemImage = require('./ItemImage')(sequelize, DataTypes);
const Favorite = require('./Favorite')(sequelize, DataTypes);
const Comment = require('./Comment')(sequelize, DataTypes);
const Like = require('./Like')(sequelize, DataTypes);
const Share = require('./Share')(sequelize, DataTypes);
const Order = require('./Order')(sequelize, DataTypes);
const Report = require('./Report')(sequelize, DataTypes);
const Banner = require('./Banner')(sequelize, DataTypes);
const ItemLabel = require('./ItemLabel')(sequelize, DataTypes);
const House = require('./House')(sequelize, DataTypes);

// 如果有关系，可以在这里设置关联
// Item 相关
Item.belongsTo(User, { foreignKey: 'userId', as: 'author' });
Item.belongsTo(Category, { foreignKey: 'categoryId', as: 'itemCategory' });
// 如果需要 Like 模型（假设你有一个 Like 模型）
Item.hasMany(Like, {
    foreignKey: 'itemId',
    as: 'likes'
});
// 如果需要 Share/Repost 模型（假设你有一个 Share 模型）
Item.hasMany(Share, {
    foreignKey: 'itemId',
    as: 'shares'
});

// 点赞和分享相关
// Like 和 Share 的关联
Like.belongsTo(User, {
    foreignKey: 'userId',
    as: 'user'
});

Like.belongsTo(Item, {
    foreignKey: 'itemId',
    as: 'item'
});

Share.belongsTo(User, {
    foreignKey: 'userId',
    as: 'user'
});

Share.belongsTo(Item, {
    foreignKey: 'itemId',
    as: 'item'
});

// Favorite 相关
Favorite.belongsTo(User, { foreignKey: 'userId', as: 'favoritedBy' });
Favorite.belongsTo(Item, { foreignKey: 'itemId', as: 'favoritedItem' });

// Report 相关
Report.belongsTo(User, { foreignKey: 'userId', as: 'reporter' });
Report.belongsTo(Item, { foreignKey: 'itemId', as: 'reportedItem' });
Report.belongsTo(Category, { foreignKey: 'categoryId', as: 'reportCategory' }); // 注意加上这句才完整

// 从用户侧能直接拿到他发布的商品、评论、收藏、订单,反向关联
User.hasMany(Item, { foreignKey: 'userId', as: 'items' });
User.hasMany(Favorite, { foreignKey: 'userId', as: 'favorites' });
User.hasMany(Comment, { foreignKey: 'userId', as: 'comments' });
User.hasMany(Like, { foreignKey: 'userId', as: 'likes' });
User.hasMany(Share, { foreignKey: 'userId', as: 'shares' });
User.hasMany(Order, { foreignKey: 'buyerId', as: 'buyOrders' });
User.hasMany(Order, { foreignKey: 'sellerId', as: 'sellOrders' });
User.hasMany(Report, { foreignKey: 'userId', as: 'reports' }); // 可选

// 标签属于分类
ItemLabel.belongsTo(Category, { foreignKey: 'categoryId', as: 'category' });
// 分类包含多个标签（反向）
Category.hasMany(ItemLabel, { foreignKey: 'categoryId', as: 'labels' });

// House 相关
House.belongsTo(User, { foreignKey: 'userId', as: 'author' });
House.belongsTo(Category, { foreignKey: 'categoryId', as: 'category' });
User.hasMany(House, { foreignKey: 'userId', as: 'houses' });
Category.hasMany(House, { foreignKey: 'categoryId', as: 'houses' });
const db = {
    sequelize,
    User,
    Category,
    Item,
    ItemImage,
    Favorite,
    Comment,
    Order,
    Report,
    Banner,
    ItemLabel,
    Like,
    Share,
    House
};

// 调用所有模型的 associate 方法（如果有）
Object.values(db).forEach(model => {
    if (typeof model.associate === 'function') {
        model.associate(db);
    }
});
module.exports = db;
